package com.yuyou.fn.crm.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.Principals;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.crm.entity.WorkSetupOfCampus;
import com.yuyou.fn.crm.entity.WorkSetupTemplate;
import com.yuyou.fn.crm.mapper.WorkSetupOfCampusMapper;
import com.yuyou.fn.crm.service.IWorkSetupOfCampusService;
import com.yuyou.fn.crm.service.IWorkSetupTemplateService;
import com.yuyou.fn.crm.vo.WorkSetupOfCampusSaveOrUpdateVo;
import com.yuyou.fn.educational.service.ICampusService;
import com.yuyou.fn.platform.permission.DataPermission;
import com.yuyou.fn.platform.service.IAreaTeamService;
import org.apache.commons.lang3.Validate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * WorkSetupOfCampus 表数据服务层接口实现类
 *
 */
@Service
public class WorkSetupOfCampusServiceImpl extends SuperServiceImpl<WorkSetupOfCampusMapper, WorkSetupOfCampus,Long> implements IWorkSetupOfCampusService {

    @Resource
    private IWorkSetupTemplateService workSetupTemplateService ;

    @Resource
    private IAreaTeamService areaTeamService;

    @Resource
    private ICampusService campusService;

    @Override
    public void saveOrUpdateWorkSetupOfCampus(WorkSetupOfCampusSaveOrUpdateVo saveOrUpdateVo, Principals.Principal principal) {

        if(saveOrUpdateVo.getWorkSetupOfCampusId() == null ){
            Validate.notNull( saveOrUpdateVo.getWorkSetupTemplateId() ,"Required workSetupTemplateId" );
            Validate.notNull( saveOrUpdateVo.getCampusId() ,"Required campusId" );
        }

        WorkSetupTemplate workSetupTemplate = workSetupTemplateService.findById(saveOrUpdateVo.getWorkSetupTemplateId());

        WorkSetupOfCampus workSetupOfCampus = new WorkSetupOfCampus();
        BeanUtils.copyProperties(workSetupOfCampus,saveOrUpdateVo );

        workSetupOfCampus.setAreaTeamId(workSetupTemplate.getAreaTeamId());
        workSetupOfCampus.setAreaTeamName(areaTeamService.findTeamNameById(workSetupTemplate.getAreaTeamId()));

        if(workSetupOfCampus.getCampusId() != null ){
            workSetupOfCampus.setCampusName(campusService.findCampusNameByCampusId(workSetupOfCampus.getCampusId()));
        }

        workSetupOfCampus.setOperatorId(principal.id);
        workSetupOfCampus.setOperatorName(principal.name);

        if(workSetupOfCampus.getWorkSetupOfCampusId() == null ){
            checkExist(workSetupOfCampus.getCampusId());
            workSetupOfCampus.setCreateTime(TimeUtils.now());
            workSetupOfCampus.setUpdateTime(TimeUtils.now());
            insertSelective(workSetupOfCampus);
        }else{
            workSetupOfCampus.setUpdateTime(TimeUtils.now());
            updateSelectiveById(workSetupOfCampus);
        }

        
    }

    private void checkExist(Long campusId) {
        Example<WorkSetupOfCampus> example = WorkSetupOfCampus.createExample();
        example.createCriteria().andEqualTo("campusId" ,campusId );
        if(countByExample(example) > 0 ){
            throw new BusException("这个校区已经设置");
        }
    }

    @Override
    public List<WorkSetupOfCampus> findWorkSetupOfCampusByAreaTeamId(DataPermission dataPermission, Long areaTeamId, Long campusId) {
        Example<WorkSetupOfCampus> example = WorkSetupOfCampus.createExample();
        example.orderBy("createTime").desc();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillConditionForAreaTeamPermission(criteria,dataPermission.getAreaTeamIds());
        if(areaTeamId != null ){
            criteria.andEqualTo("areaTeamId" , areaTeamId );
        }
        if(areaTeamId != null ){
            criteria.andEqualTo("campusId" , campusId );
        }
        return findListByExample(example);
    }

    @Override
    public List<WorkSetupOfCampus> findWorkSetupOfCampusOfOpen(List<Long> campusIds) {
        Example<WorkSetupOfCampus> example = WorkSetupOfCampus.createExample();
        example.createCriteria().andEqualTo("status" , 1 ).andIn("campusId" , campusIds );
        return findListByExample(example);
    }
}